package com.dong.es.entity.searchparam.query.match;

import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;

/**
 * @description: Es 精确匹配 相关参数
 * @author: 陈瑞东
 * @create: 2022-06-23 11:47
 */
public class EsTermQueryParam extends EsMatchQueryParam<Object>{
    private static final long serialVersionUID = -5138183067180098040L;

    public EsTermQueryParam(String field, Object value) {
        super(field, value);
    }


    @Override
    public void loadTo(BoolQuery.Builder target) {
        if (value instanceof String) {
            target.must(must -> must.term(term -> term.field(field).value(FieldValue.of(value.toString()))));
        } else if (value instanceof Integer) {
            target.must(must -> must.term(term -> term.field(field).value(FieldValue.of(Integer.parseInt(value.toString())))));
        } else if (value instanceof Long) {
            target.must(must -> must.term(term -> term.field(field).value(FieldValue.of(Long.parseLong(value.toString())))));
        } else if (value instanceof Float) {
            target.must(must -> must.term(term -> term.field(field).value(FieldValue.of(Float.parseFloat(value.toString())))));
        } else if (value instanceof Double) {
            target.must(must -> must.term(term -> term.field(field).value(FieldValue.of(Double.parseDouble(value.toString())))));
        }
    }
}
